import threading
import time
import serial


class SerialPort:
    def __init__(self, port, band_rate, datasize, stop, check):
        self.timeout = 2
        self.port = port
        self.bandrate = band_rate
        self.databits = datasize
        self.stopbit = stop
        self.check = check
        self.lserial = None

    def start(self):
        self.lserial = serial.Serial()

        self.lserial.port = self.port
        self.lserial.baudrate = self.bandrate
        self.lserial.stopbits = self.stopbit
        self.lserial.bytesize = self.databits
        self.lserial.parity = self.check
        self.lserial.timeout = self.timeout
        self.lserial.open()
        print(self.lserial.portstr)
        if self.lserial.is_open:
            return True
        else:
            return False

    def send(self, str):
        if self.lserial.is_open is False:
            print("myserial is not opened")
        llen = self.lserial.write(str)
        return llen

    def stop(self):
        self.lserial.close()


if __name__ == '__main__':
    myport = SerialPort("com11", 115200, 8, 1, 'N')
    myport.start()
    myport.send(bytes([0x3a,0x12]))
    myport.stop()
